﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Churchtoolkit.Web.Shell.Presenters;
using Churchtoolkit.Utils.ErrorProvider;
using System.Web.Security;
using System.Collections;
using Churchtoolkit.Web.Shell.Views;
using Churchtoolkit.DataAccess.Entities;

namespace Churchtoolkit.Web
{
    public partial class ModulesView : Page<ModulesPresenter>, IModulesView
    {
        #region [ Events ]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillMemberModulesList();

                FillModulesList();
            }
        }

        protected void gv_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridView gv = (GridView)sender;
            int moduleId = Convert.ToInt32(gv.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            ButtonAction(e.CommandName, moduleId);
            FillMemberModulesList();
            FillModulesList();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            FillModulesList();
        }
        #endregion

        #region [ Helper ]
        public void ButtonAction(string command, int moduleId)
        {
            if (command == "Config")
            {
                
            }
            else if (command == "Remove")
            {
                this.Presenter.RemoveModule(moduleId);
            }
            else if (command == "Add")
            {
                this.Presenter.AddModule(moduleId);
            }
        }
        private void FillMemberModulesList()
        {
            this.gvMemberModules.DataSource = this.Presenter.GetCurrentMemberModules();
            this.gvMemberModules.DataBind();
        }
        private void FillModulesList()
        {
            this.gvSearchResult.DataSource = this.Presenter.SearchforModules(this.txtSearch.Text);
            this.gvSearchResult.DataBind();
        }
        #endregion
    }
}